package function;

import View.FillInformationWindow;
import View.RegisterWindow;
import View.window;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.regex.Pattern;

public class RegisterFunction {
    private static String SendYzm;

    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }

    public static void registerExec(RegisterWindow registerWindow, FillInformationWindow fillInformationWindow) {
        ((Button) $(registerWindow, "goButton")).setOnAction(event -> {
            registerWindow.resetErrorTip();
            String act = ((TextField) $(registerWindow, "actText")).getText();
            String psw = ((PasswordField) $(registerWindow, "pswText")).getText();
            String rePsw = ((PasswordField) $(registerWindow, "rePswText")).getText();
            String email = ((TextField) $(registerWindow, "emailText")).getText();
            String yzm = ((TextField) $(registerWindow, "yzmText")).getText();
            String correctAct = "^[0-9,a-z,A-Z,\\u4e00-\\u9fa5]{1,15}$";
            String correctPsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctRePsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctEmail = "^[a-zA-Z1-9_]\\w+@\\w+\\.(com|cn)";
            if (act.equals("") || psw.equals("") || rePsw.equals("") || email.equals("") || yzm.equals("")) {
                if (act.equals("")) {
                    registerWindow.setErrorTip("actTip", "！未选择账号");
                }
                if (psw.equals("")) {
                    registerWindow.setErrorTip("pswTip", "！未输入密码");
                }
                if (rePsw.equals("")) {
                    registerWindow.setErrorTip("rePswTip", "！未输入密码");
                }
                if (email.equals("")) {
                    registerWindow.setErrorTip("emailTip", "！未输入邮箱账号");
                }
                if (yzm.equals("")) {
                    registerWindow.setErrorTip("yzmTip", "！未输入验证码");
                }
            } else if (!Pattern.matches(correctAct, act) || !Pattern.matches(correctPsw, psw) || !Pattern.matches(correctRePsw, rePsw) || !Pattern.matches(correctEmail, email)) {

                if (!Pattern.matches(correctAct, act)) {
                    registerWindow.setErrorTip("actTip", "账号是长度不超过15位的中文和英文和数字");
                }
                if (!Pattern.matches(correctPsw, psw)) {
                    registerWindow.setErrorTip("pswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctRePsw, rePsw)) {
                    registerWindow.setErrorTip("rePswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctEmail, email)) {
                    registerWindow.setErrorTip("emailTip", "邮箱格式错误");
                }
            } else if (!psw.equals(rePsw)) {
                registerWindow.setErrorTip("pswTip", "两次密码不一致");
                registerWindow.setErrorTip("rePswTip", "两次密码不一致");
            } else {
                if (yzm.equals(SendYzm)) {
                    fillInformationWindow.show();
                    registerWindow.hide();
                } else {
                    registerWindow.setErrorTip("yzmTip", "验证码错误");
                }
            }

        });
        ((Button) $(registerWindow, "getYzmButton")).setOnAction(actionEvent -> {
            registerWindow.resetErrorTip();
            String act = ((TextField) $(registerWindow, "actText")).getText();
            String psw = ((PasswordField) $(registerWindow, "pswText")).getText();
            String rePsw = ((PasswordField) $(registerWindow, "rePswText")).getText();
            String email = ((TextField) $(registerWindow, "emailText")).getText();
            String correctAct = "^[0-9,a-z,A-Z,\\u4e00-\\u9fa5]{1,15}$";
            String correctPsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctRePsw = "^[a-z,A-Z,0-9]{6,20}$";
            String correctEmail = "^[a-zA-Z1-9_]\\w+@\\w+\\.(com|cn)";
            if (act.equals("") || psw.equals("") || rePsw.equals("") || email.equals("")) {
                System.out.println("进入空");
                if (act.equals("")) {
                    registerWindow.setErrorTip("actTip", "！未输入账号");
                }
                if (psw.equals("")) {
                    registerWindow.setErrorTip("pswTip", "！未输入密码");
                }
                if (rePsw.equals("")) {
                    registerWindow.setErrorTip("rePswTip", "！未输入密码");
                }
                if (email.equals("")) {
                    registerWindow.setErrorTip("emailTip", "！未输入邮箱账号");
                }
            } else if (!Pattern.matches(correctAct, act) || !Pattern.matches(correctPsw, psw) || !Pattern.matches(correctRePsw, rePsw) || !Pattern.matches(correctEmail, email)) {
                System.out.println("进入格式");
                if (!Pattern.matches(correctAct, act)) {
                    registerWindow.setErrorTip("actTip", "账号是长度不超过15位的中文和英文和数字");
                }
                if (!Pattern.matches(correctPsw, psw)) {
                    registerWindow.setErrorTip("pswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctRePsw, rePsw)) {
                    registerWindow.setErrorTip("rePswTip", "密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(correctEmail, email)) {
                    registerWindow.setErrorTip("emailTip", "邮箱格式错误");
                }
            } else if (!psw.equals(rePsw)) {
                System.out.println("进入密码不一致");
                registerWindow.setErrorTip("pswTip", "两次密码不一致");
                registerWindow.setErrorTip("rePswTip", "两次密码不一致");
            } else {
                try {
                    SendYzm = randomCode();
                    send_email(email, SendYzm);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
                registerWindow.setErrorTip("yzmTip", "验证码已发送，请注意查收");
            }
        });
        ((Button) $(registerWindow, "resetButton")).setOnAction(actionEvent -> {
            registerWindow.clear();
            registerWindow.resetErrorTip();
        });
        ((Button)$(registerWindow,"getAccountButton")).setOnAction(event->{
            ((TextField) $(registerWindow, "actText")).setText(function.account_maker.account_maker());
        });
    }

    //获取验证码
    public static String randomCode() {
        return (int) ((Math.random() * 9 + 1) * 100000) + "";
    }

    public static void send_email(String receiver, String yzm) throws AddressException, MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("1623427947@qq.com"));
        InternetAddress to = new InternetAddress(receiver);
        message.setRecipient(MimeMessage.RecipientType.TO, to);
        message.setSubject("Let' chat!");
        message.setText(yzm);
        Transport transport = session.getTransport();
        transport.connect("1623427947@qq.com", "rqgwzfyehksscice");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
